package cc.comac.mom.datahandlers.entity.config;

import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.bean.config.EquipDataConfigParamBean;
import cc.comac.mom.dao.entity.config.EquipDataConfigDao;
import cc.comac.mom.entity.config.EquipDataConfigEntity;
import cc.comac.mom.redis.subscribe.MomEquipRedisChannel;
import cc.comac.mom.redis.subscribe.SubscribeMessage;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class EquipDataConfigDataHandler extends ServiceImpl<EquipDataConfigDao, EquipDataConfigEntity> {
	@Autowired
	private EquipDataConfigDao equipDataConfigDao;
	@Autowired
	private EquipConnectConfigDataHandler equipConnectConfigDataHandler;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean saveDataConfig(EquipDataConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		// 校验主数据重复性
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dev_number", obj.getDevNumber());
		map.put("row_status", 1);
		List<EquipDataConfigEntity> selectByMap = equipDataConfigDao.selectByMap(map);
		if (selectByMap.size() > 0) {
			throw new RuntimeException("设备编号：" + obj.getDevNumber() + "已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(obj);
		int insert = equipDataConfigDao.insert(obj);
		if (insert < 1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}
	public ResultBean updateDataConfig(EquipDataConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		EquipDataConfigEntity equipDevEntity = equipDataConfigDao.selectById(obj.getId());
		if (equipDevEntity == null) {
			throw new RuntimeException("修改的数据不存在！");
		}
		if (!equipDevEntity.getDevNumber().equals(obj.getDevNumber())) {
			throw new RuntimeException("设备编号:" + equipDevEntity.getDevNumber() + "不能修改");
		}
		baseEntityDataUtil.updateBaseEntityData(obj);
		int update = equipDataConfigDao.updateById(obj);
		if (update < 1) {
			throw new RuntimeException("修改失败");
		}

		SubscribeMessage subscribeMessage=new SubscribeMessage();
		subscribeMessage.setDataId(obj.getId());
		subscribeMessage.setNewIp(obj.getServiceIp());
		if(!obj.getServiceIp().equals(equipDevEntity.getServiceIp())){
			subscribeMessage.setOldIp(equipDevEntity.getServiceIp());
			subscribeMessage.setCrud(MomEquipRedisChannel.EQUIP_INFO_CHANNEL_ADD);
		}else{
			subscribeMessage.setCrud(MomEquipRedisChannel.EQUIP_INFO_CHANNEL_UPDATE);
		}
		stringRedisTemplate.convertAndSend(MomEquipRedisChannel.EQUIP_INFO_CHANNEL,JSONObject.toJSONString(subscribeMessage));


		return resultBean;
	}

	public ResultBean delete(EquipDataConfigEntity obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDataConfigEntity equipDevEntity = equipDataConfigDao.selectById(obj.getId());
		if (equipDevEntity == null) {
			throw new RuntimeException("删除的数据不存在！");
		}
		equipDevEntity.setRowStatus(0);
		int updateById = equipDataConfigDao.updateById(equipDevEntity);
		if (updateById < 1) {
			throw new RuntimeException("删除失败！");
		}
		//删除设备下的连接
		equipConnectConfigDataHandler.deleteByDataId(obj.getId());

		SubscribeMessage subscribeMessage=new SubscribeMessage();
		subscribeMessage.setDataId(obj.getId());
		subscribeMessage.setNewIp(obj.getServiceIp());
		subscribeMessage.setCrud(MomEquipRedisChannel.EQUIP_INFO_CHANNEL_DELETE);
		stringRedisTemplate.convertAndSend(MomEquipRedisChannel.EQUIP_INFO_CHANNEL,JSONObject.toJSONString(subscribeMessage));

		return resultBean;
	}

	public EquipDataConfigEntity getById(long id) {
		EquipDataConfigEntity bean = equipDataConfigDao.selectById(id);
		return bean;
	}

	public EquipDataConfigParamBean selectBySomeFields(EquipDataConfigParamBean obj) {
		if (obj.getPageNum() == null) {
			obj.setPageNum(1);
		}

		if (obj.getPageSize() == null) {
			obj.setPageSize(10);
		}
		QueryWrapper<EquipDataConfigEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.orderByDesc("creation_time");
		if (StringUtils.isNotEmpty(obj.getDevNumber())) {
			queryWrapper.like("dev_number", obj.getDevNumber());
		}
		if (StringUtils.isNotEmpty(obj.getDevName())) {
			queryWrapper.like("dev_name", obj.getDevName());
		}
		Page<EquipDataConfigEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipDataConfigEntity> selectPage = equipDataConfigDao.selectPage(page, queryWrapper);

		obj.setEntityList(selectPage.getRecords());
		obj.setPageCount((int) selectPage.getPages());
		obj.setTotalQuantity((int) selectPage.getTotal());
		return obj;
	}
}
